#include "stdafx.h"
#include "CppUnitTest.h"

#include <HSModel/HeightMapCoordinates.h>
#include <HSModel/Ground.h>
#include <HSModel/SharedConstants.h>


using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace HSModelTests
{
	TEST_CLASS(HeightMapCoordinatesTests)
	{
	public:
		
		TEST_METHOD(TestHeightMapIndexesFinding)
		{
			auto ind = HeightMapUtils::FindHeightMapIndexes(TVector2(1.5, 1.5), TVector2(1, 1), 1.0);
      Assert::AreEqual(0, (int) ind.first);
      Assert::AreEqual(0, (int) ind.second);

      auto ind2 = HeightMapUtils::FindHeightMapIndexes(TVector2(1.5, 1.5), TVector2(1, 1), 0.3);
      Assert::AreEqual(1, (int) ind2.first);
      Assert::AreEqual(1, (int) ind2.second);
		}

    TEST_METHOD(ShouldEstimateIfCellBelowTriangle)
		{
			size_t i_x = 1;
		  size_t i_y = 1;
      TTriangle2 tested_triangle = TTriangle2( TVector2(10.25, 10.25), TVector2(0, 10.25), TVector2(10.25, 0)); 
      bool restriction;
      restriction = HeightMapUtils::CellBelowTriangle(i_x, i_y, tested_triangle);
      Assert::IsTrue(restriction);
      
      tested_triangle = TTriangle2( TVector2(10, 10), TVector2(0, 10.25), TVector2(10.25, 0)); 
      restriction = HeightMapUtils::CellBelowTriangle(i_x, i_y, tested_triangle);
      Assert::IsFalse(restriction);

      tested_triangle = TTriangle2( TVector2(20, 20), TVector2(30, 20), TVector2(20, 30)); 
      restriction = HeightMapUtils::CellBelowTriangle(i_x, i_y, tested_triangle);
      Assert::IsFalse(restriction);
    
      tested_triangle = TTriangle2( TVector2(20, 10), TVector2(30, 10), TVector2(20, 20)); 
      restriction = HeightMapUtils::CellBelowTriangle(i_x, i_y, tested_triangle);
      Assert::IsFalse(restriction);  

      tested_triangle = TTriangle2( TVector2(10, 10), TVector2(20, 10), TVector2(10, 20)); 
      restriction = HeightMapUtils::CellBelowTriangle(i_x, i_y, tested_triangle);
      Assert::IsTrue(restriction); 
    }

    TEST_METHOD(ShouldReturnCenterOfCell)
      {
      HeightMap hm(2, 2);
      hm.Set(0, 0, 1);
      hm.Set(1, 0, 1);
      hm.Set(0, 1, 1);
      hm.Set(1, 1, 1);

      Ground ground(hm, GroundConstants::CELL_SIZE, GroundConstants::WATER_LEVEL);

      TVector3 world_point(0, 0, 1);
      TVector3 o_coords;
      HeightMapUtils::GetObjectPlacementCoords(o_coords, ground, world_point);

      double expected_x = GroundConstants::ORIGIN_X + GroundConstants::CELL_SIZE / 2;
      double expected_y = GroundConstants::ORIGIN_Y + GroundConstants::CELL_SIZE / 2;
      double expected_z = GroundConstants::ORIGIN_Z + GroundConstants::CELL_SIZE;
      Assert::AreEqual(expected_x, o_coords.X());
      Assert::AreEqual(expected_y, o_coords.Y());
      Assert::AreEqual(expected_z, o_coords.Z());
      }
	};
}